package com.zjf.carpark.controller;

import com.zjf.carpark.entities.Announcements;
import com.zjf.carpark.service.impl.AnnouncementsServiceImpl;
import com.zjf.carpark.utils.ResponseData;
import com.zjf.carpark.vo.AnnounceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.zjf.carpark.utils.CodeAndMessage.*;

/**
 * 张建飞
 * 公告控制类
 */
@CrossOrigin
@Controller
@RequestMapping("announcement")
public class AnnouncementController {

    @Autowired
    private AnnouncementsServiceImpl announcementsService;

    private ResponseData responseData = new ResponseData(CODE200, ERROR);
    private int result = 0;

    /**
     * 新增公告
     *
     * @param data
     * @param
     * @return
     */
    @RequestMapping("addAnnouncement")
    @ResponseBody
    public ResponseData addAnnouncement(@RequestBody Map<String, Announcements> data) {
        Announcements announcements = data.get("Announcements");
        System.out.println(announcements);
        announcements.setReleaseTime(new Date());
        result = announcementsService.save(announcements);
        if (result != 0) {
            responseData = new ResponseData(CODE200, SUCCESS);
        }
        return responseData;
    }


    /**
     * 删除公告
     *
     * @param
     * @return
     */
    @RequestMapping("deleteAnnouncement")
    @ResponseBody
    public ResponseData deleteAnnouncement(@RequestBody Map<String, Integer[]> data) {
        Integer[] announcementsId = data.get("announcementsId");
        result = announcementsService.deleteAnnouncements(announcementsId);
        if (result != 0) {
            responseData = new ResponseData(CODE200, SUCCESS);
        }
        return responseData;
    }

    /**
     * 公告的模糊查询通用方法接口
     *
     * @param
     * @return
     */
    @RequestMapping("showAll")
    @ResponseBody
    public ResponseData showAll(AnnounceVO announceVO) {
        List<Announcements> list = announcementsService.showAllByMsg(announceVO);
        int count = announcementsService.totalCount(announceVO);
        if (list != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
            responseData.putDataValue("Announcements", list);
            responseData.setCount(count);
        }
        return responseData;
    }

    /**
     * 查询所有已发布的公告
     *
     * @return
     */
    @RequestMapping("showAllRelease")
    @ResponseBody
    public ResponseData showAllRelease() {
        List<Announcements> announcements = announcementsService.selectAll();
        if (announcements != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
            responseData.putDataValue("Announcements", announcements);
        }
        return responseData;
    }

    /**
     * 公告详情查看
     *
     * @param data
     * @return
     */
    @RequestMapping("showOne")
    @ResponseBody
    public ResponseData showOne(@RequestBody Map<String, Integer> data) {

        Integer announcementsId = data.get("announcementsId");
        Announcements list = announcementsService.selectById(announcementsId);
        if (list != null) {
            responseData = new ResponseData(CODE200, SUCCESS);
            responseData.putDataValue("Announcement", list);
        }
        return responseData;
    }


    /**
     * 修改公告
     *
     * @param announcements
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public ResponseData update(Announcements announcements) {
        result = announcementsService.update(announcements);
        if (result != 0) {
            responseData = new ResponseData(CODE200, SUCCESS);
        }
        return responseData;
    }
}
